package 并发编程14.cow;

import java.security.Key;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author idea
 * @data 2019/2/4
 */
public class CopyOnWriteMap<K,V> implements Map<K,V> ,Cloneable {

    private volatile Map<K,V> internalMap;

    public CopyOnWriteMap(){
        internalMap=new HashMap<K,V>();
    }

    @Override
    public V get(Object key) {
        return internalMap.get(key);
    }

    @Override
    public V put(K key, V value) {
        synchronized (this){
            Map<K, V> newMap = new HashMap<K, V>(internalMap);
            V val=newMap.put(key,value);
            internalMap=newMap;
            return val;
        }
    }

    @Override
    public V remove(Object key) {
        return null;
    }

    @Override
    public int size() {
        return internalMap.size();
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    @Override
    public boolean containsKey(Object key) {
        return false;
    }

    @Override
    public boolean containsValue(Object value) {
        return false;
    }



    @Override
    public void putAll(Map<? extends K, ? extends V> m) {

    }

    @Override
    public void clear() {

    }

    @Override
    public Set<K> keySet() {
        return null;
    }

    @Override
    public Collection<V> values() {
        return null;
    }

    @Override
    public Set<Entry<K, V>> entrySet() {
        return null;
    }


    public static void main(String[] args) {
        CopyOnWriteMap<String,Integer> copyOnWriteMap=new CopyOnWriteMap<>();
        copyOnWriteMap.put("idea",1);
        copyOnWriteMap.put("idea",2);
        copyOnWriteMap.put("idea",3);
        System.out.println(copyOnWriteMap.get("idea"));
    }
}
